<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="tiles" uri="http://struts.apache.org/tags-tiles"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="c_rt" uri="http://java.sun.com/jstl/core_rt"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fmt_rt" uri="http://java.sun.com/jstl/fmt_rt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>
<%@ taglib prefix="x_rt" uri="http://java.sun.com/jstl/xml_rt"%>
<%@ taglib prefix="wr_fn" uri="http://njo.org/taglib/functions"%>
<c:import url="/common/javascript.jsp"/>

<script type="text/javascript">
  <!--
  var fieldSetItems = ${wr_fn:toJsObject(fieldSetList)};
  
  var fieldItems = $H({
<c:forEach items="${fieldList}" var="item" varStatus="itemStatus">
    <c:set var="f_l_" value="${wr_fn:isEmpty(item.field_size) ? '' : '('}"/>
    <c:set var="f_r_" value="${wr_fn:isEmpty(item.field_size) ? '' : ')'}"/>
    ${item.field_physical_name}: ['${item.field_type}${f_l_}${wr_fn:isEmpty(item.field_size) ? '' : item.field_size}${f_r_}', '${item.field_logic_name}', '${item.field_description}']<c:if test="${!itemStatus.last}">,</c:if> 
</c:forEach>
  });


  function chk_click(chk_box, name) {
    var form = $('f2');
    var field_chks = form.getInputs('checkbox', "chk_" + name);
    var field_hiddens = form.getInputs('hidden', name);
    var pos = field_chks.indexOf(chk_box);
    if (pos!=-1) {
        field_hiddens[pos].value = chk_box.checked ? "on" : "";
    }
  }

  var prefix = "field_";
  
  var fieldCount = ${wr_fn:length(wr_fn:isEmpty(fieldSet.fields) ? 0 : fieldSet.fields)};
  function addField() {
    var trElement = createRow(fieldCount, "", "", "", "", "");
    var containerElement = $('_fields');
    if (fieldCount==0) {
        var xElement = $(prefix + "x");
        containerElement.removeChild(xElement);
    }
    fieldCount++;
    containerElement.appendChild(trElement);
  }

  function delField(row_no) {
    var i;
    var validatefieldsElement = $('_fields');
    var old_tr_id = prefix + row_no;
    var oldElement = $(old_tr_id);
    if (!oldElement) {
        return;
    }
    validatefieldsElement.removeChild(oldElement);

    var form = $('f2');
    var field_inputs1 =  form.getInputs('hidden', 'fields$field_physical_name');
    var field_inputs2 =  form.getInputs('hidden', 'fields$allow_null');
    var field_inputs3 =  form.getInputs('hidden', 'fields$primary_key');
    var field_inputs4 =  form.getInputs('hidden', 'fields$foreign_key_field_set_name');
    var field_inputs5 =  form.getInputs('hidden', 'fields$foreign_key_field_name');

    for (i=row_no+1;i<fieldCount;i++) {
      var nextElement = $(prefix + i);
      if (!nextElement) {
        break;
      }
      var curfield_physical_name = field_inputs1[i-1].value;
      var curallow_null = field_inputs2[i-1].value;
      var curprimary_key = field_inputs3[i-1].value;
      var foreign_key_field_set_name = field_inputs4[i-1].value;
      var foreign_key_field_name = field_inputs5[i-1].value;
      var newTrElement = createRow((i-1), curfield_physical_name, curallow_null, curprimary_key, foreign_key_field_set_name, foreign_key_field_name);
      validatefieldsElement.replaceChild(newTrElement, nextElement);
    }

    fieldCount--;

    if (fieldCount==0) {
        var trElementx = document.createElement("tr");
        trElementx.setAttribute("id", prefix + "x");
        trElement.className = "even";
        var tdElementx = document.createElement("td");
        tdElementx.className = "center";
        tdElementx.setAttribute("colSpan", "8");
        tdElementx.innerHTML = "没有设定字段";
        trElementx.appendChild(tdElementx);
        validatefieldsElement.appendChild(trElementx);
    }
  }

  function editFK(event, row_no) {
    var editor = $("fk_editor");
    editor.innerHTML = "";

    var form = $('f2');
    var field_inputs = form.getInputs('hidden', 'fields$field_physical_name');
    var field_names = [];
    field_inputs.each(function(item) {
        field_names.push(item.value);
    });

    if (fieldSetItems.size()>0) {
      var tableElement = document.createElement("table");
      tableElement.className = "forum";
      tableElement.setAttribute("width", "98%");

      var colElement;
      colElement = document.createElement("col");
      colElement.setAttribute("width", "150");
      tableElement.appendChild(colElement);
      colElement = document.createElement("col");
      colElement.setAttribute("width", "*");
      tableElement.appendChild(colElement);

      var tbodyElement;
      tbodyElement = document.createElement("tbody");
      tableElement.appendChild(tbodyElement);

      var trElement;
      var tdElement;
      trElement = document.createElement("tr");

      tdElement = document.createElement("th");
      tdElement.innerHTML ="Field Set Name";
      trElement.appendChild(tdElement);

      tdElement = document.createElement("th");
      tdElement.innerHTML ="Description";
      trElement.appendChild(tdElement);

      tbodyElement.appendChild(trElement);

      trElement = document.createElement("tr");
      trElement.className = (0%2==0) ? "even" : "odd";
      tdElement = document.createElement("td");
      tdElement.innerHTML = "<a href=\"javascript:selectedFK(" + row_no + ", \'\', \'\')\">无</a>";
      tdElement.setAttribute("colSpan", "3");
      trElement.appendChild(tdElement);
      tbodyElement.appendChild(trElement);

      fieldSetItems.each(function(pair, index) {
        trElement = document.createElement("tr");
        trElement.className = ((index+1)%2==0) ? "even" : "odd";
        tdElement = document.createElement("td");
        //tdElement.innerHTML = "<a href=\"javascript:editFK1(event, " + row_no + ", " + index + ");\">" + pair['field_set_physical_name'] + "</a>";
        tdElement.innerHTML = "<a name=\"title\">" + pair['field_set_physical_name'] + "</a>";
        Event.observe(tdElement, "click", function(e){editFK1(e, row_no , index)});
        trElement.appendChild(tdElement);
        tdElement = document.createElement("td");
        tdElement.innerHTML = pair['field_set_description'];
        tdElement.className = "left";
        trElement.appendChild(tdElement);
        tbodyElement.appendChild(trElement);
      });
    }
    editor.appendChild(tableElement);
    editor.style.display='';
    editor.style.left=Event.pointerX(event);
    editor.style.top=Event.pointerY(event);
  }

  function editFK1(event, row_no, field_set_item_index) {
    var editor = $("fk1_editor");
    editor.innerHTML = "";

    var form = $('f2');
    var field_inputs = form.getInputs('hidden', 'fields$field_type');
    var curType = field_inputs[row_no].value;

    if (fieldSetItems.size()>0) {
        var field_set_obj = fieldSetItems[field_set_item_index];
        var field_set_fields = field_set_obj['fields'];
        if (field_set_fields && field_set_fields.size()>0) {
          var field_set_fields_x = [];
          field_set_fields.each(function(pair, index){
            if (pair['primary_key']=="on" && curType==fieldItems[pair['field_physical_name']][0]) {
                field_set_fields_x.push(pair);
            }
          });

          if (field_set_fields_x && field_set_fields_x.size()>0) {
            var tableElement = document.createElement("table");
            tableElement.className = "forum";
            tableElement.setAttribute("width", "98%");

            var colElement;
            colElement = document.createElement("col");
            colElement.setAttribute("width", "150");
            tableElement.appendChild(colElement);
            colElement.setAttribute("width", "150");
            tableElement.appendChild(colElement);
            colElement = document.createElement("col");
            colElement.setAttribute("width", "*");
            tableElement.appendChild(colElement);

            var tbodyElement;
            tbodyElement = document.createElement("tbody");
            tableElement.appendChild(tbodyElement);

            var trElement;
            var tdElement;
            trElement = document.createElement("tr");

            tdElement = document.createElement("th");
            tdElement.innerHTML ="Field Name";
            trElement.appendChild(tdElement);

            tdElement = document.createElement("th");
            tdElement.innerHTML ="Field Type";
            trElement.appendChild(tdElement);

            tdElement = document.createElement("th");
            tdElement.innerHTML ="Description";
            trElement.appendChild(tdElement);

            tbodyElement.appendChild(trElement);
            field_set_fields_x.each(function(pair, index){
              if (curType==fieldItems[pair['field_physical_name']][0]) {
                trElement = document.createElement("tr");
                trElement.className = (index%2==0) ? "even" : "odd";

                tdElement = document.createElement("td");
                tdElement.innerHTML = "<a href=\"javascript:selectedFK(" + row_no + ", \'" + field_set_obj['field_set_physical_name'] + "\', \'" + pair['field_physical_name'] + "\')\">" + pair['field_physical_name'] + "</a>";
                trElement.appendChild(tdElement);

                tdElement = document.createElement("td");
                tdElement.innerHTML = fieldItems[pair['field_physical_name']][0];
                tdElement.className = "left";
                trElement.appendChild(tdElement);

                tdElement = document.createElement("td");
                tdElement.innerHTML = fieldItems[pair['field_physical_name']][2];
                tdElement.className = "left";
                trElement.appendChild(tdElement);
                tbodyElement.appendChild(trElement);
              }
            });
            editor.appendChild(tableElement);
            editor.style.display='';
            editor.style.left=Event.pointerX(event);
            editor.style.top=Event.pointerY(event);
          }
        }
    }
  }
  
  function selectedFK(row_no, fk_field_set_physical_name, fk_field_physical_name) {
    $("fk_editor").style.display = "none";
    $("fk1_editor").style.display = "none";
    var form = $('f2');
    var field_inputs1 =  form.getInputs('hidden', 'fields$field_physical_name');
    var curfield_physical_name = field_inputs1[row_no].value;
    var field_inputs2 =  form.getInputs('hidden', 'fields$allow_null');
    var curallow_null = field_inputs2[row_no].value;
    var field_inputs3 =  form.getInputs('hidden', 'fields$primary_key');
    var curprimary_key = field_inputs3[row_no].value;

    var oldTrElement = $(prefix + row_no);
    var newTrElement = createRow(row_no, curfield_physical_name, curallow_null, curprimary_key, fk_field_set_physical_name, fk_field_physical_name);
    var containerElement = $('_fields');
    containerElement.replaceChild(newTrElement, oldTrElement);
  }

  function deleteFK(row_no) {
    var form = $('f2');
    var field_inputs1 =  form.getInputs('hidden', 'fields$field_physical_name');
    var curfield_physical_name = field_inputs1[row_no].value;
    var field_inputs2 =  form.getInputs('hidden', 'fields$allow_null');
    var curallow_null = field_inputs2[row_no].value;
    var field_inputs3 =  form.getInputs('hidden', 'fields$primary_key');
    var curprimary_key = field_inputs3[row_no].value;

    var oldTrElement = $(prefix + row_no);
    var newTrElement = createRow(row_no, curfield_physical_name, curallow_null, curprimary_key, "", "");
    var containerElement = $('_fields');
    containerElement.replaceChild(newTrElement, oldTrElement);
  }

  function editField(event, row_no) {
    var editor = $("field_editor");
    editor.innerHTML = "";

    var form = $('f2');
    var field_inputs = form.getInputs('hidden', 'fields$field_physical_name');
    var field_names = [];
    field_inputs.each(function(item) {
        field_names.push(item.value);
    });

    if (fieldItems.size()>0) {
      var tableElement = document.createElement("table");
      tableElement.className = "forum";
      tableElement.setAttribute("width", "98%");

      var colElement;
      colElement = document.createElement("col");
      colElement.setAttribute("width", "150");
      tableElement.appendChild(colElement);
      colElement = document.createElement("col");
      colElement.setAttribute("width", "*");
      tableElement.appendChild(colElement);

      var tbodyElement;
      tbodyElement = document.createElement("tbody");
      tableElement.appendChild(tbodyElement);

      var trElement;
      var tdElement;
      trElement = document.createElement("tr");

      tdElement = document.createElement("th");
      tdElement.innerHTML ="Field Name";
      trElement.appendChild(tdElement);

      tdElement = document.createElement("th");
      tdElement.innerHTML ="Field Type";
      trElement.appendChild(tdElement);

      tdElement = document.createElement("th");
      tdElement.innerHTML ="Description";
      trElement.appendChild(tdElement);

      tbodyElement.appendChild(trElement);

      var icounter = 0;
      fieldItems.each(function(pair) {
        if (field_names.indexOf(pair.key)==-1) {
          trElement = document.createElement("tr");
          trElement.className = (icounter%2==0) ? "even" : "odd";
          tdElement = document.createElement("td");
          tdElement.innerHTML = "<a href=\"javascript:selectedField(\'" + pair.key + "\', " + row_no + ")\">" + pair.key + "</a>";
          trElement.appendChild(tdElement);
          tdElement = document.createElement("td");
          tdElement.innerHTML = pair.value[0];
          tdElement.className = "left";
          trElement.appendChild(tdElement);
          tdElement = document.createElement("td");
          tdElement.innerHTML = pair.value[2];
          tdElement.className = "left";
          trElement.appendChild(tdElement);
          tbodyElement.appendChild(trElement);
          icounter++;
        }
      });
    }
    editor.appendChild(tableElement);
    editor.style.display='';
    editor.style.left=Event.pointerX(event);
    editor.style.top=Event.pointerY(event);
  }

  function selectedField(field_physical_name, row_no) {
    $("field_editor").style.display = "none";
    var oldTrElement = $(prefix + row_no);
    var newTrElement = createRow(row_no, field_physical_name, "", "", "", "");
    var containerElement = $('_fields');
    containerElement.replaceChild(newTrElement, oldTrElement);
  }

  function hideEditor(event, editor) {
    var obj = $(editor);
    var offsets = Position.positionedOffset(obj);
    var obj_l = offsets[0];
    var obj_t = offsets[1];
    var obj_w = Element.getWidth(obj);
    var obj_h = Element.getHeight(obj);
    var printer_x = Event.pointerX(event);
    var printer_y = Event.pointerY(event);
    if (printer_x<=obj_l || printer_x>=obj_l+obj_w
        || printer_y<=obj_t || printer_y>=obj_t+obj_h) {
      obj.style.display = "none";
    }
  }

  function writeRow(row_no,field_physical_name, allow_null, primary_key, foreign_key_field_set_name, foreign_key_field_name) {
    var row = createRow(row_no,field_physical_name, allow_null, primary_key, foreign_key_field_set_name, foreign_key_field_name);
    var elementsContainer = $('_fields');
    elementsContainer.appendChild(row);
  }
  
  function createRow(row_no,field_physical_name, allow_null, primary_key, foreign_key_field_set_name, foreign_key_field_name) {
    var tr_id = prefix + row_no;
    trElement = document.createElement("tr");
    trElement.setAttribute("id", tr_id);
    trElement.className = (row_no%2==0) ? "even" : "odd";
    var tdElement = document.createElement("td");
    tdElement.innerHTML = "<a title=\"删除\" href=\"javascript:delField(" + row_no + ");\"><img src=\"${pageContext.request.contextPath}/images/remove.png\" border=\"0\"/></a>";
    tdElement.className = "center";
    trElement.appendChild(tdElement);
    tdElement = document.createElement("td");
//    tdElement.innerHTML = field_physical_name + "<input type=\"hidden\" name=\"fields$field_physical_name\" value=\"" + field_physical_name + "\">" +
//                          "<img onclick=\"javascript:editField(event, " + row_no + ");\" src=\"${pageContext.request.contextPath}/images/edit.png\" border=\"0\"/>";
    tdElement.innerHTML = "<a name=\"title\">" + field_physical_name + "</a><input type=\"hidden\" name=\"fields$field_physical_name\" value=\"" + field_physical_name + "\">";
    tdElement.className = "left";
    Event.observe(tdElement, "click", function(e){editField(e, row_no)});
    trElement.appendChild(tdElement);

    var field_logic_name = (fieldItems[field_physical_name] ? fieldItems[field_physical_name][1] : "");
    tdElement = document.createElement("td");
    tdElement.innerHTML = "<a href=\"${pageContext.request.contextPath}/processor.loadfield.tiles?name=${item.name}\">" + field_logic_name + "</a>";
    tdElement.className = "left";
    trElement.appendChild(tdElement);

    tdElement = document.createElement("td");
    var field_type = (fieldItems[field_physical_name] ? fieldItems[field_physical_name][0] : "") + "<input type=\"hidden\" name=\"fields$field_type\" value=\"" + (fieldItems[field_physical_name] ? fieldItems[field_physical_name][0] : "") + "\">";
    tdElement.innerHTML = field_type;
    trElement.appendChild(tdElement);

    tdElement = document.createElement("td");
    tdElement.innerHTML = "<input type=\"checkbox\" name=\"chk_fields$allow_null\" " + ((allow_null=="on") ? "checked" : "") + " onclick=\"javascript:chk_click(this, \'fields$allow_null\');\"><input type=\"hidden\" name=\"fields$allow_null\" value=\"" + allow_null + "\">";
    tdElement.className = "center";
    trElement.appendChild(tdElement);

    tdElement = document.createElement("td");
    tdElement.innerHTML = "<input type=\"checkbox\" name=\"chk_fields$primary_key\" " + ((primary_key=="on") ? "checked" : "") + " onclick=\"javascript:chk_click(this, \'fields$primary_key\');\"><input type=\"hidden\" name=\"fields$primary_key\" value=\"" + primary_key + "\">";
    tdElement.className = "center";
    trElement.appendChild(tdElement);

    tdElement = document.createElement("td");
    tdElement.innerHTML = foreign_key_field_set_name + "." + foreign_key_field_name + "<input type=\"hidden\" name=\"fields$foreign_key_field_set_name\" value=\"" + foreign_key_field_set_name + "\"><input type=\"hidden\" name=\"fields$foreign_key_field_name\" value=\"" + foreign_key_field_name + "\">";
    tdElement.className = "left";
    Event.observe(tdElement, "click", function(e){editFK(e,row_no)});
    trElement.appendChild(tdElement);

    tdElement = document.createElement("td");
    var field_description = (fieldItems[field_physical_name] ? fieldItems[field_physical_name][2] : "");
    tdElement.innerHTML = field_description;
    tdElement.className = "left";
    trElement.appendChild(tdElement);
    return trElement;
  }

  -->
</script>
